//
//  HMVisitorView.swift
//  MyWeibo
//
//  Created by apple on 16/4/17.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

// 定义协议
// 需要继承于一个基协议 ： NSObjectProtocol
// 如果不继承，不能使用 weak 修饰
@objc protocol HMVisitorViewDelegate:NSObjectProtocol{
    
    
   //设置代理方法 这些方法都是必须实现的
   func didClickregisterButton()
    
   func didClickLonginButton()
    
    
}

class HMVisitorView: UIView {
    
    //创建一个遵守协议的代理属性
    weak var delegate : HMVisitorViewDelegate?
    

   override init(frame: CGRect) {
        
        super.init(frame: frame)
    
    //初始化加载视图
    setupUI()

    }

    //注册按钮点击事件
 @objc private func didClickregisterButton(){
        //注册代理方法的响应和执行
    delegate?.didClickregisterButton()
    }
    //登录按钮点击事件
 @objc private func didClickLonginButton(){
    //登录代理方法的响应和执行
    delegate?.didClickLonginButton()
    
    }
    
   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    
    //创建一个方法来判断传入的参数
    func setVisitorInfo(imagName:String?,message:String?){
        
        
        if let name = imagName,mes = message{
            
            iconView.image = UIImage(named:name)
            
            messageLabel.text = mes
        }else{
            
            startanima()
        }
        
    }
    
    func startanima(){
    
        // 显示圆盘
        circleView.hidden = false
        //开启动画
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        //设置弧度
        anima.toValue = M_PI * 2
        //设置动画时间
        anima.duration = 15
        //设置转圈次数
        anima.repeatCount = MAXFLOAT
        //设置切换默认设置  一直可以转
        anima.removedOnCompletion = false
        //添加动画到calayer
        circleView.layer.addAnimation(anima, forKey: nil)
    
    
    }
    
   private func setupUI(){
        
        //设置背景颜色
    backgroundColor = UIColor(white: 237/255.0, alpha: 1)
    
    //添加图片
    addSubview(circleView)
    addSubview(maskIconView)
    addSubview(iconView)
    addSubview(messageLabel)
    addSubview(registerButton)
    addSubview(loginButton)
    
    
    //添加小房子约束
    
    iconView.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(self)
    }
 
    //关闭系统约束
//    iconView.translatesAutoresizingMaskIntoConstraints = false
//    addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    
    //添加转盘约束
    circleView.snp_makeConstraints { (make) -> Void in
        
        make.center.equalTo(self)
    }
//    //关闭系统约束
//    circleView.translatesAutoresizingMaskIntoConstraints = false
//    addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
    
    //添加文字约束
    messageLabel.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(circleView.snp_centerX)
        
        make.top.equalTo(circleView.snp_bottom).offset(15)
    }
//    //关闭系统
//    messageLabel.translatesAutoresizingMaskIntoConstraints = false
//    
//    addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
//
//    addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 10))
    
   //添加注册按钮约束
    registerButton.snp_makeConstraints { (make) -> Void in
        
        make.leading.equalTo(messageLabel)
        make.top.equalTo(messageLabel.snp_bottom).offset(15)
        make.width.equalTo(100)
        
    }
//    registerButton.translatesAutoresizingMaskIntoConstraints = false
//    
//    addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 15))
//    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
  
    //添加登录按钮约束
    loginButton.snp_makeConstraints { (make) -> Void in
        
        make.trailing.equalTo(self.messageLabel.snp_trailing)
        make.top.equalTo(self.messageLabel.snp_bottom).offset(15)
        make.width.equalTo(100)
    }
    
//    loginButton.translatesAutoresizingMaskIntoConstraints = false
//    
//    addConstraint(NSLayoutConstraint(item: loginButton, attribute:NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 15
//        ))
//    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
    
    
    //添加透明背景约束和透明度
    maskIconView.snp_makeConstraints { (make) -> Void in
        
        make.centerX.equalTo(circleView)
        make.centerY.equalTo(circleView).offset(-10)
    }
    
//    maskIconView.translatesAutoresizingMaskIntoConstraints = false
//    
//    addConstraint(NSLayoutConstraint(item: maskIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//    
//    addConstraint(NSLayoutConstraint(item: maskIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
//    
    
    }
    
    
    //懒加载小房子控件
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //懒加载转盘控件
    private lazy var circleView:UIImageView = {
        
   let image = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        image.hidden = true
        
        return image
    }()
 
    //懒加载messageLabel
    private lazy var messageLabel:UILabel = {
        
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14,preferredMaxLayoutWidth: 210)
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = .Center
        
        return label
    }()
    
    //懒加载注册按钮
    private lazy var registerButton:UIButton = {
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        //设置文字
        button.setTitle("注册", forState: UIControlState.Normal)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        //添加注册点击事件
        button.addTarget(self, action: "didClickregisterButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //懒加载登录按钮
    private lazy var loginButton:UIButton = {
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 14)
        //设置文字
        button.setTitle("登录", forState: UIControlState.Normal)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        //添加登录点击事件
        button.addTarget(self, action: "didClickLonginButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()

    //懒加载半透明背景
    private lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

}
